layui.use(['tableEdit', 'table', 'form', 'laydate',"selectInput","excel"], function () {
    let layer = layui.layer //弹层
        , table = layui.table //表格
        , form = layui.form,
        $ = layui.jquery,
        laydate = layui.laydate,
        excel = layui.excel,
        selectInput = layui.selectInput;
    lay('.starttime').each(function(){
        laydate.render({
            elem: this
        });
    });
    function getAllJson(jsons) {
        let tmpitem=[]
        for (let item of jsons) {
            let tmparr={}
            tmparr["value"]=item["name"];
            tmparr["name"]=item["name"];
            if(item["users"]){
                for(let it of item["users"]){
                    item["list"].push(it)
                }
            }
            if(item["list"] && item["list"].length){
                tmparr["children"]=   getAllJson(item["list"]);
                tmpitem.push(tmparr)
            }else{
                tmpitem.push(tmparr)
            }

        }
        return tmpitem
    }
    function getxmselect(el,name, data) {
        return xmSelect.render({
            el: el,
            name: name,
            tree: {
                show: true,
                showLine: true,
                showFolderIcon:true,
                indent: 20,
                expandedKeys:false
            },
            height: 'auto',
            data: data,
            on: function ({ arr, change, isAdd }) {
                return this
            }
        })
    }

    let salesmanList=[];
    let deptList = [];
    (function () {
        let treeList = getCurrentDepartmentUser();
        if(treeList && treeList.list){
            let salesmanTree = getAllJson(treeList.list);
            salesmanList=getxmselect("#salesmanCode","salesmanCode", salesmanTree)
        }else {
            salesmanList=getxmselect("#salesmanCode","salesmanCode", [])
        }

        //获取当前用户所属部门树()
        let currentDeptTreeList = getAllUserDepartmentTree();
        if (currentDeptTreeList && currentDeptTreeList.list) {
            let deptTree = getAllJson(currentDeptTreeList.list);
            deptList = getxmselect("#departmentCode", "departmentCode", deptTree);
        } else {
            deptList = getxmselect("#departmentCode", "departmentCode", [{name: user.name, value: user.code}]);
        }
    })()

    layer.ready(function () {

        //获取币种
        $.ajax({
            url:"receivePayment/getCurrencyInfo",
            success:function(data){
                if(data.status == 200){
                    for(let i in data.data){
                        $('#currencyCode').append(new Option(data.data[i].name, data.data[i].code));
                    }
                    form.render('select');
                }
            }
        });


        //获取收款类型
        $.ajax({
            url:"receivePayment/getReceivePaymentTypeInfo",
            success:function(data){
                if(data.status == 200){
                    for(let i in data.data){
                        $('#paymentTypeCode').append(new Option(data.data[i].name, data.data[i].code));
                    }
                    form.render('select');
                }
            }
        });

    });
    form.render();//表单刷新
    let initData = [];
    table.render({
        elem: '#collection3Select'
        ,data:initData
        ,toolbar: '#collection3Toolbar'
        ,totalRow: true
        ,cellMinWidth: 100
        ,cols: [[
            {field: 'numbers',width: 50,type:'numbers',title:'序号', totalRowText: '合计'}
            ,{field:'claimSalesman', title:'业务员',sort:true,align:'center',width: 170}
            ,{field:'claimCustomer', title:'认领客户名称',sort:true,align:'center',width: 360}
            ,{field:'customerName', title:'登记客户名称',sort:true,align:'center',width: 360}
            ,{field:'currency', title:'币种',sort:true,width: 170,align:'center'}
            ,{field:'typeName', title:'收款类型',sort:true,align:'center',width: 170}
            ,{field: 'amountClaimedUSD', title: '认领金额USD', sort: true, width: 170, align: 'center', totalRow: true}
            ,{field:'amountClaimed', title:'认领金额',sort:true,align:'center',width: 170, totalRow: true}
        ]]
        ,id: 'maininformationtableid'
        ,height: $(document).height() - $('#collection3Select').offset().top - 75,
        //开启分页
        page: true,
        limits: [300,600,900],
    });

    //搜索提交
    form.on('submit(submitform)', function(data){
        table.reload('maininformationtableid', {
            page: {
                limit:300,
                curr: 1
            },
            url:'receivePayment/collectionStatistics3ListByPage',
            where:  data.field
        }, 'data');
        return false;
    });



    form.on('submit(exportData)', function (queryReq) {
        let index = layer.load(2, {
            shade: [0.4, '#000'],
            content: '加载中...',
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '40px',
                    'width': '200px'
                });
            }
        });
        $.ajax({
            data: queryReq.field,
            url:"receivePayment/getContractStatisticsThirdExport",
            success: function(res) {
                if(res.status == 200){
                    //配置列宽
                    let colConf = excel.makeColConfig({
                        'A': 120,
                        'AH': 120
                    }, 120);
                    excel.exportExcel({
                        sheet1: res.data.data
                    }, '业务员年度认领金额统计.xlsx', 'xlsx', {
                        extend: {
                            '!cols': colConf
                        }
                    });
                }else {
                    layer.msg(res.msg, {icon: 5});
                }
                layer.close(index);
                return false;
            }
        });
        return false;
    });
    document.getElementById('clear').onclick = function(){
        salesmanList.setValue([ ]);
        deptList.setValue([ ]);
    };

});

